
// <copyright file="DynamicObjectEmptyCheckHelperExtensions.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The dynamic object empty check helper extensions.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    using SubhadraSolutions.Sharp.Utils.Reflection;

    /// <summary>
    /// The dynamic object empty check helper extensions.
    /// </summary>
    public static class DynamicObjectEmptyCheckHelperExtensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// The dynamic is empty.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <param name="trimStrings">
        /// The trim strings.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool DynamicIsEmpty<T>(this T obj, bool trimStrings = false)
        {
            if (obj == null)
            {
                return true;
            }

            return DynamicObjectEmptyCheckHelper<T>.IsEmpty(obj, trimStrings);
        }

        /// <summary>
        /// The dynamic is empty.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <param name="trimStrings">
        /// The trim strings.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public static bool DynamicIsEmpty(this object obj, bool trimStrings = false)
        {
            if (obj == null)
            {
                return true;
            }

            Type t = typeof(DynamicObjectEmptyCheckHelper<>).MakeGenericType(new[] { obj.GetType() });
            return (bool)t.InvokeMember("IsEmpty", BindingFlags.InvokeMethod, null, null, new[] { obj, trimStrings });
        }

        /// <summary>
        /// The dynamic remove empty elements.
        /// </summary>
        /// <param name="list">
        /// The list.
        /// </param>
        /// <param name="trimStrings">
        /// The trim strings.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        public static void DynamicRemoveEmptyElements<T>(this IList<T> list, bool trimStrings = false)
        {
            Type t = typeof(DynamicObjectEmptyCheckHelper<>).MakeGenericType(new[] { typeof(T) });
            t.InvokeMember("RemoveEmptyElements", BindingFlags.InvokeMethod, null, null, new object[] { list, trimStrings });
        }

        #endregion Public Methods and Operators
    }
}
